package mse.eai.testes;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.xpath.NodeSet;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class TesteSOAP {

public static void main(String[] args) throws XPathExpressionException{
	
	// SOURCE
	// change this!
	InputSource inputSource = new InputSource("/_LIXO/GetWinnerListSOAPREsponse.xml");
	// InputSource inputSource = new InputSource(new StringReader(xml));
	
	XPath xPath=XPathFactory.newInstance().newXPath();

	//XPathExpression  xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
	//String title = xPathExpression.evaluate(inputSource);			         
	
	String expression="//return";
	NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
	
	int count = nodes.getLength();

	for (int i = 0; i < count; i++) {
		Node winnerNode = nodes.item(i); 
		
		System.out.println(winnerNode);
		
		Node amountNode = (Node) xPath.evaluate("lindenEuros", winnerNode, XPathConstants.NODE);
		Node userIdNode = (Node) xPath.evaluate("userId", winnerNode, XPathConstants.NODE);
		
		int amount = Integer.parseInt(amountNode.getTextContent());
		int userId = Integer.parseInt(userIdNode.getTextContent());
		
		System.out.println("USer " + userId + " has won " + amount);
	}
	
	
}

}
